package com.salefinder.client;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SaleFinder implements EntryPoint {

	private RowLayout rowLayout;
	private LayoutContainer currentScreen;
	private FlexTable table;

	public static SaleFinder master = null;
	
	
	public void onModuleLoad() {
		
		Command dummy = new Command(){

			@Override
			public void execute() {
				//Do nothing
				
			}};
		
		RootPanel rootPanel = RootPanel.get();

		LayoutContainer hp = new LayoutContainer();
		TableLayout layout = new TableLayout();
		layout.setCellHorizontalAlign(Style.HorizontalAlignment.CENTER);
		layout.setCellVerticalAlign(VerticalAlignment.TOP);
		layout.setWidth("100%");
		hp.setLayout(layout);
		
		
		table = new FlexTable();
		table.setStyleName("myapppanel");
		table.setHeight("600px");		
					
		rootPanel.setSize("100%", "100%");

		Label lblNewLabel = new Label("Sale Finder");
		lblNewLabel.setHeight(25);
		table.setWidget(0,0, lblNewLabel);
		table.getFlexCellFormatter().setColSpan(0, 0, 2);
		

		MenuBar bar = new MenuBar();
		bar.setStyleName("demo-MenuBar");
		bar.addItem("Home", new Command() {
		      public void execute() {
		    	  currentScreen = Controller.getHomeScreen();
					table.setWidget(2, 1, currentScreen);
		      }
		    });
		    
		bar.addItem("|", dummy);
		bar.addItem("Submit products", new Command() {
		      public void execute() {
		    	  currentScreen = Controller.getAddItemScreen();
					table.setWidget(2, 1, currentScreen);
		      }
		    });		
		bar.addItem("|",dummy);
		bar.addItem("Login", new Command() {
		      public void execute() {
		    	  currentScreen = Controller.getLoginScreen();
					table.setWidget(2, 1, currentScreen);
		      }
		    });
		
		bar.addItem("|",dummy);
		bar.addItem("Manage my list", new Command() {
		      public void execute() {
		    	  currentScreen = Controller.getLoginScreen();
					table.setWidget(2, 1, currentScreen);
		      }
		    });	

		table.setWidget(1, 0, bar);
		table.getFlexCellFormatter().setColSpan(1, 0, 2);

		table.setWidget(2, 0, Controller.getPreferenceScreen());	
		table.getCellFormatter().setWidth(2, 0, "50px");
		table.getCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
		this.currentScreen = Controller.getHomeScreen();

		table.setWidget(2, 1, currentScreen);
		table.getCellFormatter().setWidth(2, 1, "700px");
		table.getCellFormatter().setAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
		
		hp.add(table);
		rootPanel.add(hp);
		
		
		master = this;
	}

	public void updateCurrentScreen(LayoutContainer newScreen){
		 currentScreen = newScreen;
			table.setWidget(2, 1, currentScreen);
			table.getCellFormatter().setAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP);
	}


}
